﻿using System;
using System.Text.RegularExpressions;

namespace Magellan.Routing
{
    public abstract class RegexBasedSegmentRecognizer : SegmentRecognizer
    {
        private readonly Regex _formatRegex;

        protected RegexBasedSegmentRecognizer(Regex formatRegex)
        {
            _formatRegex = formatRegex;
        }

        public override Segment Recognise(string value, RouteValueDictionary defaults, RouteValueDictionary constraints)
        {
            var match = _formatRegex.Match(value);
            if (!match.Success)
                return null;

            return Build(match, defaults, constraints);
        }

        protected abstract Segment Build(Match match, RouteValueDictionary defaults, RouteValueDictionary constraints);
    }
}